home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Sample Code Update 01⁄96 / Fragment Tool / Sources / AppleEventStuff.c next >
Encoding:
C/C++ Source or Header  |  1995-11-20  |  3.7 KB  |  175 lines  |  [TEXT/MPCC]

  1. /*
  2.     File:        AppleEventStuff.c
  3.  
  4.     Contains:    Handlers for the 4 "required" events
  5.  
  6.     Written by:    Chris White, Developer Technical Support
  7.     
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9.     
  10.     Change History (most recent first):
  11.     
  12.                   9/28/95    CW        First release
  13.  
  14. */
  15.  
  16.  
  17. #ifndef __MIXEDMODE__
  18.     #include <MixedMode.h>
  19. #endif
  20.  
  21. #ifndef __GESTALT__
  22.     #include <Gestalt.h>
  23. #endif
  24.  
  25. #ifndef __APPLEEVENTS__
  26.     #include <AppleEvents.h>
  27. #endif
  28.  
  29.  
  30.  
  31.  
  32.  
  33. #ifndef __FRAGMENTTOOL__
  34.     #include "FragmentTool.h"
  35. #endif
  36.  
  37. #ifndef __PROTOTYPES__
  38.     #include "Prototypes.h"
  39. #endif
  40.  
  41. #ifndef __APPLEEVENTSTUFF__
  42.     #include "AppleEventStuff.h"
  43. #endif
  44.  
  45.  
  46.  
  47.  
  48.  
  49. //
  50. // Prototypes for those routines which are private to this source file
  51. //
  52.  
  53. static pascal OSErr HandleOapp ( AEDescList* aevt, AEDescList* reply, long refCon );
  54. static pascal OSErr HandleQuit ( AEDescList* aevt, AEDescList* reply, long refCon );
  55. static pascal OSErr HandleOdoc ( AEDescList* aevt, AEDescList* reply, long refCon );
  56. static pascal OSErr HandlePdoc ( AEDescList* aevt, AEDescList* reply, long refCon );
  57.  
  58.  
  59.  
  60.  
  61. void InstallAppleEventHandlers ( void )
  62. {
  63.     OSErr    theErr;
  64.     int32    theResult;
  65.  
  66.     theErr = Gestalt ( gestaltAppleEventsAttr, &theResult );
  67.     if ( theErr == noErr )
  68.     {    
  69.         AEInstallEventHandler ( kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc ( HandleOapp ), 0, false );
  70.         AEInstallEventHandler ( kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc ( HandleOdoc ), 0, false );
  71.         AEInstallEventHandler ( kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerProc ( HandlePdoc ), 0, false );
  72.         AEInstallEventHandler ( kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc ( HandleQuit ), 0, false );
  73.     }
  74.     
  75.     return;
  76.     
  77. }    // InstallAppleEventHandlers
  78.  
  79.  
  80. //
  81. // When we get an "oapp" event, we've been launched without any
  82. // document to open. So, we'll create an untitled document.
  83. //
  84. pascal OSErr HandleOapp ( AEDescList* aevt, AEDescList* reply, long refCon )
  85. {
  86.     DoNewDocument ( );
  87.     
  88.     return noErr;
  89. }
  90.  
  91.  
  92.  
  93. //
  94. // The "odoc" and "pdoc" messages contain a list of aliases as the direct paramater.
  95. // This means that we'll need to extract the list, count the list's elements, and
  96. // then process each file in turn.
  97. //                                                                                                 */
  98. pascal OSErr HandleOdoc ( AEDescList* aevt, AEDescList* reply, long refCon )
  99. {
  100.     AEDesc        fileListDesc = {'NULL', NULL};
  101.     long        numFiles;
  102.     DescType    actualType;
  103.     long        actualSize;
  104.     AEKeyword    actualKeyword;
  105.     FSSpec        oneFile;
  106.     long        index;
  107.     OSErr        err;
  108.                         
  109.     
  110.     // Extract the list of aliases into fileListDesc
  111.     err = AEGetKeyDesc( aevt, keyDirectObject, typeAEList, &fileListDesc );
  112.     if ( err ) goto CleanupAndBail;
  113.         
  114.     // Count the list elements
  115.     err = AECountItems( &fileListDesc, &numFiles);
  116.     if ( err ) goto CleanupAndBail;
  117.     
  118.     // Now get each file from the list and process it.
  119.     // Even though the event contains a list of alises, the Apple Event Manager
  120.     // will convert each alias to an FSSpec if we ask it to.
  121.     for ( index = 1; index <= numFiles; index ++ )
  122.     {
  123.         err = AEGetNthPtr ( &fileListDesc, index, typeFSS, &actualKeyword,
  124.                             &actualType, (Ptr) &oneFile, sizeof ( oneFile ), &actualSize );
  125.                             
  126.         #if DEBUGGING
  127.         if ( err )
  128.             DebugStrNum ( "\p AEGetNthPtr returned ", err );
  129.         #endif
  130.         
  131.         if ( err )
  132.             break;
  133.         
  134.         err = OpenDocument ( &oneFile, nil );
  135.         
  136.         #if DEBUGGING
  137.         if ( err )
  138.             DebugStrNum ( "\p OpenDocument returned ", err );
  139.         #endif
  140.         
  141.         if ( err )
  142.             break;
  143.     }
  144.     
  145.     
  146.     // Since we want to free all the storage this routine allocates,
  147.     // we'll continue execution here.
  148.     
  149. CleanupAndBail:
  150.     
  151.     AEDisposeDesc ( &fileListDesc );
  152.     return err;
  153.     
  154. } // HandleOdoc
  155.  
  156.  
  157.  
  158. pascal OSErr HandlePdoc ( AEDescList* aevt, AEDescList* reply, long refCon )
  159. {
  160.     return errAEEventNotHandled;
  161. }
  162.  
  163.  
  164.  
  165. pascal OSErr HandleQuit ( AEDescList* aevt, AEDescList* reply, long refCon )
  166. {
  167.     gQuit = true;
  168.     
  169.     return noErr;
  170. }
  171.  
  172.  
  173.  
  174.  
  175.